import {onBeforeRouteUpdate, useRoute} from 'vue-router'
import {getCategoryAPI} from '@/apis/category.js'
import {onMounted, ref} from 'vue'

export function useCategory(){
	const categoryData = ref({})
	const route = useRoute()
	const getCategory = async (id = route.params.id) => {
		const res = await getCategoryAPI(id)
		categoryData.value = res.result
	}

	onMounted(() => getCategory())

	// 路由缓存问题精细化解决
	onBeforeRouteUpdate((to) => {
		// 使用最新的路由参数请求最新的分类数据
		getCategory(to.params.id)
	})

	return {
		categoryData
	}
}
